package com.jt.ktdemo.ui

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.jt.ktdemo.R
import com.jt.ktdemo.databinding.ActivityEditBinding

class EditActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var binding: ActivityEditBinding
    private lateinit var type: String
    private lateinit var name:String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_edit)
        binding.activity = this
        initView()
    }

    private fun initView() {
        binding.tvModuleSave.setOnClickListener(this)
        binding.ivModuleBack.setOnClickListener(this)

        type = intent.getStringExtra("module_type").toString()
        when (type) {
            "name" -> {
                binding.tvModuleTitle.text = "Edit name"
                binding.etModuleEmail.hint = "Please enter your name"
            }
            "nickName" -> {
                binding.tvModuleTitle.text = "Edit nickName"
                binding.etModuleEmail.hint = "Please enter your nickName"
            }
        }
        name = intent.getStringExtra("module_name").toString()
        if(!TextUtils.isEmpty(name)){
            binding.etModuleEmail.setText(name)
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tvModuleSave -> {
                var result = binding.etModuleEmail.text.toString().trim()
                if (!TextUtils.isEmpty(result)) {
                    val intent = Intent()
                    intent.putExtra("module_result", result)
                    intent.putExtra("module_type",type)
                    setResult(RESULT_OK,intent)
                    finish()
                }
            }
            R.id.ivModuleBack -> {
                finish()
            }
        }
    }
}